package com.guang.executor.schduleexecutor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author lg
 * @Description 看下这里的现象
 *
 * 2022-10-28 15:01:32.102 [pool-1-thread-1] INFO  c.g.e.schduleexecutor.ScheduleExecutorTestThree - 当前线程开始来执行任务
 * 2022-10-28 15:01:37.129 [pool-1-thread-1] INFO  c.g.e.schduleexecutor.ScheduleExecutorTestThree - 当前线程执行任务已经超过了5秒钟
 * 2022-10-28 15:01:39.142 [pool-1-thread-1] INFO  c.g.e.schduleexecutor.ScheduleExecutorTestThree - 当前线程开始来执行任务
 *
 * @date 2022/10/28 14:42
 */
public class ScheduleExecutorTestThree {
    private final static Logger logger = LoggerFactory.getLogger(ScheduleExecutorTestThree.class);
    static ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1);
    public static void main(String[] args) {
        logger.info("main线程执行代码开始时间");
        scheduledThreadPoolExecutor.scheduleWithFixedDelay(()->{
            logger.info("当前线程开始来执行任务");
            long startTime = System.currentTimeMillis();
            long nowTime = startTime;
            while ((nowTime-startTime)<5000){
                try {
                    TimeUnit.MILLISECONDS.sleep(100);
                    nowTime = System.currentTimeMillis();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            logger.info("当前线程执行任务已经超过了5秒钟");
        },5,2, TimeUnit.SECONDS);
        // 延迟五秒钟之后启动一次代码，然后每隔2秒钟执行一次代码，会造成任务堆积
        logger.info("main线程继续向下执行");
    }
}
